package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.OrderFlowInfo;
import com.yy.jhl.ui.manager.contract.OrderFlowContract;
import com.yy.jhl.ui.manager.presenter.OrderFlowPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/7.
 */

public class OrderFlowActivity extends BaseActivity<OrderFlowPresenter> implements OrderFlowContract.View {

    @BindView(R.id.order_name_text)
    TextView orderNameText;
    @BindView(R.id.order_code_text)
    TextView orderCodeText;
    @BindView(R.id.order_content_text)
    TextView orderContentText;
    @BindView(R.id.xiadan_icon)
    ImageView xiadanIcon;
    @BindView(R.id.xiadan_line)
    View xiadanLine;
    @BindView(R.id.xiadan_title_text)
    TextView xiadanTitleText;
    @BindView(R.id.xiadan_time_text)
    TextView xiadanTimeText;
    @BindView(R.id.xiadan_parent)
    LinearLayout xiadanParent;
    @BindView(R.id.wuliu_icon)
    ImageView wuliuIcon;
    @BindView(R.id.wuliu_line)
    View wuliuLine;
    @BindView(R.id.wuliu_title_text)
    TextView wuliuTitleText;
    @BindView(R.id.wuliu_new_text)
    TextView wuliuNewText;
    @BindView(R.id.batch_parent)
    LinearLayout batchParent;
    @BindView(R.id.wuliu_parent)
    LinearLayout wuliuParent;
    @BindView(R.id.yanshou_icon)
    ImageView yanshouIcon;
    @BindView(R.id.yanshou_line)
    View yanshouLine;
    @BindView(R.id.yanshou_title_text)
    TextView yanshouTitleText;
    @BindView(R.id.yanshou_parent)
    LinearLayout yanshouParent;
    @BindView(R.id.over_icon)
    ImageView overIcon;
    @BindView(R.id.over_title_text)
    TextView overTitleText;
    @BindView(R.id.over_parent)
    LinearLayout overParent;
    String orderId = "";
    @BindView(R.id.order_gys_text)
    TextView orderGysText;
    @BindView(R.id.wuliu_progress_text)
    TextView wlProgressText;
    @BindView(R.id.batch_text)
    TextView batchText;
    @BindView(R.id.gys_parent)
    LinearLayout gysParent;
    @BindView(R.id.content_parent)
    LinearLayout contentParent;
    OrderFlowInfo orderFlowInfo;
    @BindView(R.id.cgs_wuliu_parent)
    LinearLayout cgsWuliuParent;
    @BindView(R.id.gys_wuliu_text)
    TextView gysWuliuText;
    @BindView(R.id.gys_wuliu_parent)
    LinearLayout gysWuliuParent;
    @BindView(R.id.wuliu_hint_text)
    TextView wuliuHintText;
    @BindView(R.id.no_wuliu_parent)
    LinearLayout noWuliuParent;
    @BindView(R.id.item_one_name_text)
    TextView itemOneNameText;
    @BindView(R.id.item_one_time_text)
    TextView itemOneTimeText;
    @BindView(R.id.item_one_parent)
    LinearLayout itemOneParent;
    @BindView(R.id.item_two_name_text)
    TextView itemTwoNameText;
    @BindView(R.id.item_two_time_text)
    TextView itemTwoTimeText;
    @BindView(R.id.order_product_more_text)
    TextView orderProductMoreText;
    @BindView(R.id.order_batch_num_text)
    TextView orderBatchNumText;
    @BindView(R.id.item_two_parent)
    LinearLayout itemTwoParent;
    @BindView(R.id.yanshou_content_parent)
    LinearLayout yanshouParentParent;
    private int currentLoginType = 1;
    String addressId = "";

    public static void startAction(Context mContext, String orderId) {
        Intent intent = new Intent(mContext, OrderFlowActivity.class);
        intent.putExtra("orderId", orderId);
        mContext.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_order_flow;
    }

    @Override
    public void initView() {
        currentLoginType = SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE);
        contentParent.setVisibility(View.GONE);
        orderId = getIntent().getStringExtra("orderId");
        showTitle(currentLoginType == 2 ? "监管流程" : "任务流程", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        if (currentLoginType == 1){
            gysParent.setVisibility(View.GONE);
        }

    }

    @Override
    protected void onStart() {
        mPresenter.getProjectDetail(orderId);
        super.onStart();
    }

    @OnClick({R.id.xiadan_parent, R.id.wuliu_parent, R.id.yanshou_parent, R.id.over_parent, R.id.gys_wuliu_text, R.id.wuliu_hint_text,R.id.order_product_more_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.xiadan_parent:
                break;
            case R.id.wuliu_parent:
                if (orderFlowInfo != null)
                    BatchManagerActivity.startAction(mContext, orderId, orderFlowInfo.getTitle(), orderFlowInfo.getOrder_sn());
                break;
            case R.id.yanshou_parent:

                break;
            case R.id.over_parent:
                break;
            case R.id.gys_wuliu_text:
                break;
            case R.id.wuliu_hint_text:
                break;
            case R.id.order_product_more_text:
                ProductListViewWebViewActivity.startAction(mContext,orderId,"");
                break;
        }
    }

    @Override
    public void getProjectDetail(OrderFlowInfo info) {
        contentParent.setVisibility(View.VISIBLE);
        orderFlowInfo = info;
        orderNameText.setText(info.getTitle());
        orderCodeText.setText(info.getOrder_sn());
        orderBatchNumText.setText(info.getOrder_batch().getBatch_num());
        orderContentText.setText(StringUtil.isEmpty(info.getOrder_info()) ? "" : info.getOrder_info().replace("--","\n"));
        orderGysText.setText(info.getCompany_supply_name());
        xiadanTimeText.setText(info.getCreate_time());
        String status = info.getStatus();

        List<OrderFlowInfo.ShipAddressBean> shipAddress = orderFlowInfo.getShip_address();
        if (shipAddress != null && shipAddress.size() > 0){
            addressId = shipAddress.get(0).getOrder_contact_id();
        }
        //2:到物流监管 3：结束
        if ("2".equals(status)) {
            wuliuParent.setClickable(true);
            yanshouParent.setClickable(false);
            wuliuNewText.setVisibility(View.VISIBLE);
            wuliuLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
            yanshouIcon.setImageResource(R.mipmap.yanshou_no_icon);
            yanshouLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
            overIcon.setImageResource(R.mipmap.over__no_icon);
            overTitleText.setTextColor(mContext.getResources().getColor(R.color.gray_color));
            wlProgressText.setVisibility(View.VISIBLE);
            wlProgressText.setText((info.getOrder_ratio() == 100 ? 100 : info.getOrder_ratio()) + "%");
        } else if ("3".equals(status)){
            wuliuParent.setClickable(true);
            yanshouParent.setClickable(true);
            wuliuNewText.setVisibility(View.VISIBLE);
            wlProgressText.setVisibility(View.GONE);
            wuliuLine.setBackgroundColor(mContext.getResources().getColor(R.color.green_color));
            yanshouTitleText.setTextColor(mContext.getResources().getColor(R.color.main_color));
            yanshouLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
            yanshouIcon.setImageResource(R.mipmap.yanshou_yes_icon);
            overTitleText.setTextColor(mContext.getResources().getColor(R.color.gray_color));
            overIcon.setImageResource(R.mipmap.over__no_icon);
        } else {
            wuliuParent.setClickable(true);
            yanshouParent.setClickable(true);
            wlProgressText.setVisibility(View.GONE);
            wuliuNewText.setVisibility(View.GONE);
            wuliuLine.setBackgroundColor(mContext.getResources().getColor(R.color.green_color));
            yanshouTitleText.setTextColor(mContext.getResources().getColor(R.color.main_color));
            yanshouIcon.setImageResource(R.mipmap.yanshou_yes_icon);
            yanshouLine.setBackgroundColor(mContext.getResources().getColor(R.color.green_color));
            overTitleText.setTextColor(mContext.getResources().getColor(R.color.main_color));
            overIcon.setImageResource(R.mipmap.over__yes_icon);
        }
        String arrival = info.getOrder_batch().getArrival_batch();
        if (!StringUtil.isEmpty(arrival)) {
            yanshouParentParent.setVisibility(View.VISIBLE);
            yanshouParentParent.removeAllViews();
            String[] arrivals = arrival.split("--");
            if (arrivals != null && arrivals.length > 0) {
                int length = arrivals.length;
                for (int i = 0; i < length; i++) {
                    final String[] array = arrivals[i].split("\\#\\#");
                    if (array.length == 4) {
                        View view = LayoutInflater.from(mContext).inflate(R.layout.item_yanshou_tem,null);
                        ((TextView)view.findViewById(R.id.item_name_text)).setText(array[0]+""+array[1]);
                        ((TextView)view.findViewById(R.id.item_time_text)).setText(array[2]);
                        view.setOnClickListener(new OnNoDoubleClickListener() {
                            @Override
                            protected void onNoDoubleClick(View v) {
                                String batchId = array[3];
                                if (!StringUtil.isEmpty(addressId)){
                                    FreightForwardActivity.startAction(mContext, 3, batchId, addressId);
                                }
                            }
                        });
                        yanshouParentParent.addView(view);
                    }
                }
            }
        }else {
            yanshouParentParent.setVisibility(View.GONE);
        }
        String orderFlow = info.getOrder_batch().getLogistics_batch();
        if (!StringUtil.isEmpty(orderFlow)) {
            noWuliuParent.setVisibility(View.GONE);
            String[] flows = orderFlow.split("--");
            if (flows != null && flows.length > 0) {
                StringBuffer content = new StringBuffer();
                int length = flows.length;
                if (length >= 2) length = 2;
                for (int i = 0; i < length; i++) {
                    String[] array = flows[i].split("\\#\\#");
                    if (array.length == 3) {
                        if (i == 0){
                            itemOneParent.setVisibility(View.VISIBLE);
                            itemOneNameText.setText(array[0]+""+array[1]);
                            itemOneTimeText.setText(array[2]);
                        }else if (i == 1){
                            itemTwoParent.setVisibility(View.VISIBLE);
                            itemTwoNameText.setText(array[0]+""+array[1]);
                            itemTwoTimeText.setText(array[2]);
                        }
                    }
                }
            }
        }else {
            noWuliuParent.setVisibility(View.VISIBLE);
            if (currentLoginType == 1){
                wuliuHintText.setText("出库准备");
                wuliuHintText.setOnClickListener(new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        OutboundSubmitActivity.startAction(mContext,orderId,100);
                    }
                });
            }else {
                wuliuHintText.setText("查看物流流程");
                wuliuHintText.setOnClickListener(new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        if (orderFlowInfo != null)
                            BatchManagerActivity.startAction(mContext, orderId, orderFlowInfo.getTitle(), orderFlowInfo.getOrder_sn());
                    }
                });
            }
        }
    }

    @Override
    public void getProjectDetailFail() {

    }
}
